package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import entities.Appointment;
import entities.Calendar;
import fachlogik.Administration;
import fachlogik.TermineCellRenderer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Popup extends JFrame {

	private JPanel contentPane;
	
	private DefaultListModel appointmentModel;
	private Administration instance;
	private Calendar selectedCalendar;
	private List<Appointment> abgelaufeneAppointments;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Popup frame = new Popup();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Popup(Administration a, Calendar c, List<Appointment> liste) {
		instance = a;
		selectedCalendar = c;
		abgelaufeneAppointments = liste;
		setTitle("Reminders");
		appointmentModel = new DefaultListModel<>();
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);
		
		aktualisieren();
		final JList appList = new JList(appointmentModel);
		appList.setBounds(22, 43, 384, 158);
		appList.setCellRenderer(new TermineCellRenderer());
		appList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		contentPane.add(appList);
		
		JLabel lblAppointment = new JLabel("Appointment");
		lblAppointment.setBounds(22, 24, 93, 14);
		contentPane.add(lblAppointment);
		
		JButton btnDismiss = new JButton("Dismiss");
		btnDismiss.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				List<Appointment> list = appList.getSelectedValuesList();
				for(Appointment a : list)
				{
					if(abgelaufeneAppointments.contains(a))
					{
						abgelaufeneAppointments.remove(a);
					}
				}
				aktualisieren();
			}
		});
		btnDismiss.setBounds(242, 212, 89, 23);
		contentPane.add(btnDismiss);
		
		JButton btnReschedule = new JButton("Reschedule");
		btnReschedule.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RescheduleWindow rw = new RescheduleWindow (instance, appList.getSelectedValuesList());
				rw.setVisible(true);
			}
		});
		btnReschedule.setBounds(335, 212, 89, 23);
		contentPane.add(btnReschedule);
		
		JButton btnDismissAll = new JButton("Dismiss all");
		btnDismissAll.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
		        appointmentModel.removeAllElements();
		        dispose();
		    }
		});
		btnDismissAll.setBounds(22, 212, 89, 23);
		contentPane.add(btnDismissAll);
	}

	private void aktualisieren() {
	    appointmentModel.removeAllElements();
		for(Appointment app : abgelaufeneAppointments)
		{
			appointmentModel.addElement(app);
		}
	}
}
